/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-11-28     RT-Thread    first version
 */

#include <rtthread.h>

#define THREAD_PRIORITY 9
#define THREAD_TIMESLICE 5

#define EVENT_FLAG3 (1<<3)
#define EVENT_FLAG5 (1<<5)


#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

static struct rt_event event;
static char thread1_stack[1024];
static struct rt_thread thread1;


void hook(void *ptr,rt_size_t size){
    rt_kprintf("address %02x,size %d\n",ptr,size);
}

static void thread1_recv_event(void *param){
    int i;
    char *ptr = RT_NULL;
    for(i=0;;i++){
        rt_malloc_sethook(hook);
        ptr = rt_malloc(1<<i);
        if (ptr !=RT_NULL){
            rt_kprintf("get memory:%d byte\n",1<<i);
            //rt_free(ptr);
            //rt_kprintf("free memory:%d byte\n",1<<i);
            ptr = RT_NULL;
        }else{
            rt_kprintf("try to get %d memory fail\n",1<<i);
            return;
        }
    }
}



int main(void)
{

    rt_err_t result;
    result = rt_event_init(&event,"event",RT_IPC_FLAG_FIFO);
    if (result !=RT_EOK){
        rt_kprintf("error\n");
        return -1;
    }
    rt_thread_init(&thread1,"thread1",thread1_recv_event,RT_NULL,&thread1_stack[0],sizeof(thread1_stack),THREAD_PRIORITY - 1,THREAD_TIMESLICE);
    rt_thread_startup(&thread1);


    return RT_EOK;
}
